package core.map
{
	import core.Resource.InstancePool;
	import core.Resource.MapDefinition;
	import core.map.dataobject.MapLayer;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import mx.core.mx_internal;

	public class BaseMap extends Sprite
	{
		protected var _layerManager:LayerManager;
		protected var _moveManager:MoveManager;
		protected var _sortManager:SortManager;
		protected var _actorManager:ActorManager;
		

		public var sceneName:String = "";
		protected var _mapDef:MapDefinition;
		protected var _mapTiles:Array;
		
		protected var _groundTiles:Array;
		
		public var centerX:int = 0;
		public var centerY:int = 0;

		public function BaseMap()
		{
			_mapTiles = [];
			_layerManager = new LayerManager();
			_layerManager.init(this);
		}
		

		public function init(mapDef:MapDefinition):void
		{
			_mapDef = mapDef;
			
		}
		
		public function initMap(centerX:int,centerY:int):void
		{
			
			_groundTiles = [];
			move(centerX,centerY);
		}
		
		
		private function getDrawRect(dx:int,dy:int):Rectangle
		{
			var left:int = dx / _mapDef.colWidth;
			var right:int = left + _mapDef.colsNum;//横向4块
			if (right >= _mapDef.colsNum){
				right = _mapDef.colsNum - 1;
				left = right - _mapDef.viewColsNum;
			}
			
			var top:int = dy / _mapDef.rowHeight;
			var bottom:int = top + _mapDef.rowsNum;
			if (bottom >= _mapDef.rowsNum){
				bottom = _mapDef.rowsNum - 1;
				top = bottom - _mapDef.viewRowsNum;
			}
			
			return new Rectangle(left,top,right-left,bottom-top);
		}
		
		protected function moveBackGround(dx:int,dy:int):void{}
		protected function moveGround(dx:int,dy:int):void
		{
			var rect:Rectangle = getDrawRect(dx,dy);
			
			//获取第一块tile的偏移
			var firstTileX:int = rect.left*_mapDef.colWidth-dx;
			var firstTileY:int = rect.top*_mapDef.rowHeight-dy;
			
			var oldTiles:Array = _groundTiles;
			_groundTiles = [];
			for(var m:int = rect.left;m<=rect.right;m++)
			{
				for(var n:int = rect.top;n<=rect.bottom;n++)
				{
					var bName:String = m.toString()+"_"+n.toString();
					var np:Point = new Point(m*_mapDef.colWidth-dx,n*_mapDef.rowHeight-dy);
					if(oldTiles[bName]!=null)
					{
						var t:MapTile = oldTiles[bName] as MapTile;
						t.x = np.x;
						t.y = np.y;
						_groundTiles[bName] = t;
						delete oldTiles[bName];
					}
					else
					{
						var t:MapTile = InstancePool.instance.getMapTile();
						t.setInfo(bName,np,MapLayer.GROUND,sceneName);
						_layerManager.addChild(t,MapLayer.GROUND);
						_groundTiles[bName] = t;
					}
				}
			}
			for each(var t:MapTile in oldTiles)
			{
				_layerManager.removeChild(t,MapLayer.GROUND);
				InstancePool.instance.setMapTile(t);	
			}
			oldTiles = null;
		}
		protected function moveMain(dx:int,dy:int):void
		{
		}
		
		
		public function move(centerX:int,centerY:int):void
		{
			this.centerX = centerX;
			this.centerY = centerY;
			var dx:int = centerX-_mapDef.viewWidth/2;
			var dy:int = centerY-_mapDef.viewHeight/2;
			moveBackGround(dx,dy);
			moveGround(dx,dy);
			moveMain(dx,dy)
		}

	}
}